<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use think\facade\Session;

//用户表
class User extends Admin
{
    public function index(){
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //用户ID
            $where['id'] = $post['id']??'';
            //用户昵称
            $where['name'] = $post['name']??'';
            //一级身份
            $ptsf = [1,3,10];
            //二级身份
            $cpdl = [4,5,7];
            if(in_array($post['classs_id'],$ptsf)){
            	$where['ptsf'] = $post['classs_id'];
            }elseif(in_array($post['classs_id'],$cpdl)){
            	$where['canpinsf'] = $post['classs_id'];
            }
            if(session('login')['parmen'] == 2){
            	$where['id'] = session('login')['id'];
            }
            $wheres =[];
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k == 'identity'){
                        $wheres[] = [$k,'like',$v];
                    }else{
                        $wheres[] = [$k,'=',$v];
                    }
                }
            }
            if($wheres){
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
        	if(session('login')['parmen'] == 2){
            	$list = model($this->request->controller())->where('id','=',session('login')['id'])->paginate($limit);
	            $page = $list->render();
	            $list = $list->toArray()['data'];
	            $count = count(model($this->request->controller())->select()->toArray());
            }else{
            	$list = model($this->request->controller())->paginate($limit);
	            $page = $list->render();
	            $list = $list->toArray()['data'];
	            $count = count(model($this->request->controller())->select()->toArray());
            }
            
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function login()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post['password'] = jm($post['password']);
            $user = model('user')->where([
                ['username','=',$post['username']],
                ['password','=',$post['password']]
            ])->find();
            if($user){
            	$user = $user->toArray();
            	if($user['qxurl'] == -1){
                	// $this->assign('error','权限不足');
	                return fh(200,'error','权限不足',[]);
            	}else{
	                session('login',$user);
	                // return $this->redirect('index/index');
	                return fh(200,'success','验证通过',[]);
            	}
            }else{
	                return fh(200,'error','账号或密码错误',[]);
                // $this->assign('error','账号或密码错误');
            }
        }
        return $this->fetch();
    }
    public function register()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            if($post['status']){
                if($post['status'] == 20){
                    $list = model($this->request->controller())->where('status','=',$post['status'])->paginate($limit);
                    $page = $list->render();
                    $list = $list->toArray()['data'];
                    $count = count(model($this->request->controller())->where('status','=',$post['status'])->select()->toArray());
                }else{
                    $list = model($this->request->controller())->where('status','<>',20)->paginate($limit);
                    $page = $list->render();
                    $list = $list->toArray()['data'];
                    $count = count(model($this->request->controller())->where('status','<>',20)->select()->toArray());
                }
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function assets()
    {
        return $this->fetch();
    }
    public function zzh(){
    	if($this->request->isPost()){
    		$post = post();
    		$user = model('user')->field('id,master')->where([
    				['username','<>','admin'],
    				['master','<>',1]
    				])->select()->toArray();
    		foreach($user as $k=>$v){
    			$user[$k]['master'] = 1;
    		}
    		if(session('login')['username'] != 'admin'){
    			$user[] = ['id'=>session('login')['id'],'master'=>1];
    		}
    		$user[] = ['id'=>$post['id'],'master'=>2];
    		$model = model('user')->saveAll($user);
    		if($model){
	    		Session::delete('login');
	        	return $model;
    		}else{
    			return $model;
    		}
    	}
    	
    }
    public function updatestatus()
    {
        if($this->request->isPost()){
            $post = input('post.');
            if(isset($post['status'])){
                $model = model('user')->save(['status'=>$post['status']],['id'=>$post['id']]);
                return $this->redirect('index/index',['url'=>'user_register']);
            }
        }
    }
    public function indexs()
    {

        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            if($post){
                $list = model($this->request->controller())->where('name','like','%' . $post['name'] . '%')->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where('name','like','%' . $post['name'] . '%')->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);

        return $this->fetch();
    }
    public function setting()
    {
        $get = input('get.');
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            if(isset($post['password'])){
                $post['password'] = jm($post['password']);
                $mdoel = model('user')->save(['password'=>$post['password']],['id'=>$get['sid']]);
            }
            if(isset($post['name']) || isset($post['username'])){
                $mdoel = model('user')->save($post,['id'=>$get['sid']]);
            }
            if(isset($post['qx']))
            {
                if(isset($post['id'])){
                    $html = '';
                    foreach ($post['id'] as $k=>$v)
                    {
                        $html .= $k . ',';
                    }
                    $mdoel = model('user')->save(['qxurl'=>$html],['id'=>$get['sid']]);
                }else{
                    $mdoel = model('user')->save(['qxurl'=>'-1'],['id'=>$get['sid']]);
                }
            }
            if(isset($mdoel)){
                $post['id'] = $mdoel;
                $this->assign('success','修改成功');
            }
        }
        $user = model('user')->get($get['sid'])->toArray();
        $this->assign('user',$user);
        $allaa = model('aa')->where('level','<','3')->select()->toArray();
        if($user['qxurl'] == 0){
            $aa = model('aa')->where('level','<','3')->select()->toArray();
        }else{
            $aa = model('aa')->where([
                ['id','in',$user['qxurl']],
                ['level','<','3']
            ])->select()->toArray();
        }
        foreach ($allaa as $k=>$v)
        {
            foreach ($aa as $kk=>$vv){
                if($v['id'] == $vv['id']){
                    $allaa[$k]['checked'] = 1;
                }
            }
            if(!isset($allaa[$k]['checked'])){
                $allaa[$k]['checked'] = 2;
            }
        }
        $allaa = getTree($allaa,0);
        $this->assign('all',$allaa);
        return $this->fetch();
    }
    public function view()
    {
        $get = input('get.');
        if(isset($get['battery_order'])){
            $id = $get['battery_order'];
            $list = model($this->request->controller())->where('id','=',$id)->find()->toArray();
            $user = model($this->request->controller())->where('id','<>',$id)->select();
            if($user){
            	$user = $user->toArray();
            }else{
            	$user = [];
            }
            $this->assign('user',$user);
            $this->assign('list',$list);
        }
        if($this->request->isPost())
        {
            $post = input('post.');
            if(isset($post['po'])){
            	if($post['po'] == 2){
	                model('loan')->save(['user_id'=>$id,'name'=>$list['name'],'operationuser'=>session('login')['id'],'money'=>$post['loan'],'status'=>20]);
	                $post['loan'] = $list['loan']*1 - $post['loan']*1;
	            }else{
	                model('loan')->save(['user_id'=>$id,'name'=>$list['name'],'operationuser'=>session('login')['id'],'money'=>$post['loan'],'status'=>10]);
	                $post['loan'] = $post['loan']*1 + $list['loan']*1;
	            }
	            model('user')->save($post,['id'=>$list['id']]);
            }else{
            	model('user')->save(['parent_id'=>$post['parent_id']],['id'=>$id]);
            }
            $list = model($this->request->controller())->where('id','=',$id)->find()->toArray();
            $this->assign('list',$list);
        }
        if($list){
        	if($list['parent_id'] != 0){
        		$pa = model('user')->where('id','=',$list['parent_id'])->find();
        		if($pa){
        			$pa = $pa->toArray();
            		$this->assign('pa',$pa);
        		}
        	}
        }
        return $this->fetch();
    }
    public function team()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $id = $get['battery_order'];
        if(!$id){
            return '没有参数';
        }else{
            $this->assign('id',$id);
        }
        if(session('login')['master'] == 2){
        	if($this->request->isPost())
	        {
	            $post = input('post.');
	            $where = [];
	            //开始结束时间
	            $where['parent_id'] = $id;
	            $where['time'] = $post['time']??'';
	            //状态
	            $where['name'] = $post['name']??'';
	            $wheres =[];
	            if($where['time']){
	                list($first,$last) = explode('至',$where['time']);
	                $wheres[] = ['create_time','>',trim($first)];
	                $wheres[] = ['overtime','<',trim($last)];
	            }
	            foreach ($where as $k=>$v){
	                if(strlen($v)>0){
	                    if($k !== 'time' && $k !== 'name'){
	                        $wheres[] = [$k,'=',$v];
	                    }elseif($k == 'name'){
	                        $wheres[] = [$k,'like','%' . $v . '%'];
	                    }
	                }
	            }
	            if($wheres){
	                $list = model($this->request->controller())->where($wheres)->paginate($limit);
	                $page = $list->render();
	                $list = $list->toArray()['data'];
	                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
	            }else{
	                $list = model($this->request->controller())->where('parent_id','=',$id)->paginate($limit);
	                $page = $list->render();
	                $list = $list->toArray()['data'];
	                $count = count(model($this->request->controller())->select()->toArray());
	            }
	        }else{
	            // $list = model($this->request->controller())->where('parent_id','=',$id)->paginate($limit);
	            // $page = $list->render();
	            // $list = $list->toArray()['data'];
	            $user = model('user')->where('parent_id','=',$id)->column('id');
	            $id = getTeam($user,[]);
	            $id = implode(',',$id);
	            $list = model($this->request->controller())->where('id','in',$id)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
	            $count = count(model($this->request->controller())->select()->toArray());
	        }
        }else{
        	if($this->request->isPost())
	        {
	            $post = input('post.');
	            $where = [];
	            //开始结束时间
	            $where['parent_id'] = $id;
	            $where['time'] = $post['time']??'';
	            //状态
	            $where['name'] = $post['name']??'';
	            $wheres =[];
	            if($where['time']){
	                list($first,$last) = explode('至',$where['time']);
	                $wheres[] = ['create_time','>',trim($first)];
	                $wheres[] = ['overtime','<',trim($last)];
	            }
	            foreach ($where as $k=>$v){
	                if(strlen($v)>0){
	                    if($k !== 'time' && $k !== 'name'){
	                        $wheres[] = [$k,'=',$v];
	                    }elseif($k == 'name'){
	                        $wheres[] = [$k,'like','%' . $v . '%'];
	                    }
	                }
	            }
	            if($wheres){
	                $list = model($this->request->controller())->where($wheres)->paginate($limit);
	                $page = $list->render();
	                $list = $list->toArray()['data'];
	                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
	            }else{
	                $list = model($this->request->controller())->where('parent_id','=',$id)->paginate($limit);
	                $page = $list->render();
	                $list = $list->toArray()['data'];
	                $count = count(model($this->request->controller())->select()->toArray());
	            }
	        }else{
	            $list = model($this->request->controller())->where('parent_id','=',$id)->paginate($limit);
	            $page = $list->render();
	            $list = $list->toArray()['data'];
	            $count = count(model($this->request->controller())->select()->toArray());
	        }
        }
        
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function exits()
    {
        Session::delete('login');
        $this->redirect('user/login');
    }
    public function dc()
    {
        $arr = ['A1'=>'账号','B1'=>'昵称','C1'=>'身份','D1'=>'注册时间','E1'=>'手机号','F1'=>'积分','G1'=>'业绩','H1'=>'贷款额度','I1'=>'地址'];
        $model = model('user')->where('id','<>',1)->order('id','desc')->select()->toArray();
        $num = 2;
        if($model){
            foreach ($model as $k=>$v){
            	$sf = [];
            	if($v['ptsf'] == 10){
            		$sf[] = '普通用户';
            	}elseif($v['ptsf'] == 1){
            		$sf[] = '普通会员';
            	}elseif($v['ptsf'] == 3){
            		$sf[] = '普通代理商';
            	}
            	if($v['canpinsf'] == 4){
            		$sf[] = '产品区代理商';
            	}elseif($v['canpinsf'] == 5){
            		$sf[] = '产品市代理商';
            	}elseif($v['canpinsf'] == 7){
            		$sf[] = '产品省代理商';
            	}
            	$sf = implode(',',$sf);
                $a = [
                    'A'."$num" => $v['username'],
                    'B'."$num" => $v['name'],
                    'C'."$num" => $sf,
                    'D'."$num" => $v['create_time'],
                    'E'."$num" => $v['phone'],
                    'F'."$num" => $v['integral'],
                    'G'."$num" => $v['achievement'],
                    'H'."$num" => $v['loan'],
                    'I'."$num" => $v['region']
                ];
                $arr = array_merge($arr,$a);
                $num++;
            }
        }
        $name = date('YmdHis') . '.xls';
        $b = exportExcel($arr,$name);
    }
}
